<template>
	<view>
		<view class="ticket">
			<view class="ticket-header">
				<view class="refund">
					<text>{{cinemaName}}</text>
		
				</view>
				<view class="ticket-film">
					<view class="ticket-film-name">
						<text>{{filmData.name}}</text>
						<view class="session-date" style="color: #999;margin-top: 10rpx;">
							<text>{{getDate(startTime)}}</text>
							<text style="margin-left: 10rpx;">{{getTime(startTime)}}:00</text>
						</view>
						<view style="margin-top: 10rpx;">
							<text class="screen-name">{{screenName}}</text>
						</view>

						<view style="margin-top: 10rpx;">
							<view style="margin-top: 10rpx; position: relative;" v-if='!memberRefundParams.cardCode'>
								<image src="../../static/image/eye.png" class="close-eye" @click="showFee = true"
									v-if="!showFee"></image>
								<view class="screen-seat" v-for="(item,index) in seatsList" :key="item.seatCode">
									<text class="seat-price">{{item.rowNum}}排{{item.columnNum}}座
			<!-- 						<text
											style="color: #E53F31;margin-left: 10rpx;"
											v-if="priceList[index] > 0">{{priceList[index]}}元</text> -->
									</text>
			<!-- 						<text style="color: #E53F31;font-size: 22rpx;"
										v-if="serviceFee[index] > 0 && showFee">(含{{serviceFee[index]}}元服务费)</text>
									</text> -->
								</view>
							</view>
							<view style="margin-top: 10rpx; position: relative;" v-else>
								<image src="../../static/image/eye.png" class="close-eye" @click="showFee = true"
									v-if="!showFee"></image>
								<view class="screen-seat" v-for="(item,index) in seatsList" :key="item.seatCode">
									<text class="seat-price">{{item.rowNum}}排{{item.columnNum}}座<text
											style="color: #E53F31;margin-left: 10rpx;"
											v-if="priceList[index] > 0 && showFee">{{priceList[index]}}元</text>
									</text><text style="color: #E53F31;font-size: 22rpx;"
										v-if="serviceFee[index] > 0 && showFee">(含{{serviceFee[index]}}元服务费)</text>
									</text>
								</view>
							</view>
						</view>
					</view>
					<image class="film-img" :src="filmData.imgUrl" mode=""></image>
				</view>
				<view class="session">

					<view class="session-screen">


					</view>
				</view>
			</view>
			<view class="film-code">
				<view class="">
					<text>序列号：{{resolveCode(ticketCode.PrintNo)}}</text>
					<text>验证码：{{resolveCode(ticketCode.VerifyCode)}}</text>
				</view>
			</view>
			<view class="film-qr">
				<!-- 	<view v-if="!showCode" @click="showCodeFn">
					<text class="title">展开取票码</text>
					
				</view> -->
				<view class="show-qr" v-if="showCode">
					<view class="qr-title">
						<text>取电影票</text>
					</view>
					<view>
						<!-- <canvas style="width: 220px; height: 220px;background:pink;" canvas-id="myQrcode"></canvas> -->
						<uqr-code :text="qrCode" :show='true' :size="180"></uqr-code>
					</view>
				</view>
			</view>

			<u-popup mode='center' :round='10' :show="showPassword" @click-overlay="showPassword=false">
				<view class="input-password">
					<u--form labelPosition="left" :model="memberRefundParams" ref="uForm" labelWidth='auto'
						labelAlign='right'>
						<u-form-item label="密码：" borderBottom ref="item4">
							<u--input name="密码" password required placeholder="密码"
								v-model="memberRefundParams.password" />
						</u-form-item>
					</u--form>
					<u-button shape="circle" type="primary" @click="cardRefund"
						custom-style="margin-top:30rpx;width:60vw;height:60rpx;">确定</u-button>
				</view>
			</u-popup>
		</view>
		<view class="cinema-info">
			<view class="">
				<view class="" style="font-size: 28rpx;">
					{{cinemaName}}
				</view>
				<view class="" style="font-size: 22rpx;color: #999;">
					{{cinemaAddress}}
				</view>
			</view>
			<view class="" @click="callCinema" style="position: relative;z-index: 99999;">
				<!-- <u-icon name="phone" color='#9bbd83' size='60rpx' /> -->
			</view>
		</view>
	</view>
</template>

<script>
	import {
		queryOrderInfo,
		wxRefundTicket,
		memberRefundTicket,
		memberRefundTicketFee,
		quertTicketList,
		refundRules,
		wxPay
	} from '../../common/http/api.js'
	import UqrCode from '../uqrcode/uqrcode.vue'
	import moment from 'moment'
	export default {
		props: ["orderCode", "statusText"],
		components: {
			UqrCode
		},
		data() {
			return {
				showCode: true,
				memberRefundParams: {
					orderNo: "",
					cardCode: "",
					password: "",
					openId: uni.getStorageSync("openId"),
					fee: '',
				},
				ticketCode: {
					VerifyCode: '',
					PrintNo: '',
					openId: uni.getStorageSync("openId"),
					fee: '',
					orderNo: ''
				},
				filmData: {},
				screenName: "",
				cinemaName: '',
				cinemaAddress: '',
				cinemaPhone: '',
				startTime: '',
				seatsList: [],
				qrCode: '',
				showPassword: false,
				showRefundRules: false,
				isRefound: false,
				quertTicketList: {
					openId: uni.getStorageSync("openId"),
					pageNo: 1,
				},
				priceList: [],
				serviceFee: [],
				refundRulesList: [],
				orderTotal: 0,
				refundTips: '',
				oldOrderNo: '',
				showFee: false
			};
		},
		watch: {
			orderCode() {
				this.getDetail()
			}
				
		},

		created() {
			this.cinemaName = this.$store.state.cinemaName
			this.cinemaAddress = this.$store.state.cinemaAddress
			this.cinemaPhone = this.$store.state.cinemaPhone
			this.getDetail()

		},

		methods: {
			getDetail(){
				queryOrderInfo(this.orderCode).then(res => {
						const {
							code,
							data
						} = res
						if (code === "SUCCESS") {
							this.ticketCode = {
								VerifyCode: data.verifyCode,
								PrintNo: data.printNo,
								openId: uni.getStorageSync("openId"),
							}
				
							this.checkTime(data.startTime)
							this.filmData = data.films[0]
							this.memberRefundParams.cardCode = data.cardCode;
							this.memberRefundParams.orderNo = data.orderCode
							this.ticketCode.orderNo = data.orderCode
							let printNo = data.printNo.slice(10, 19);
							let verifyCode = data.verifyCode.slice(10, 19);
							this.qrCode = printNo + '|' + verifyCode;
							this.seatsList = data.seats
							this.startTime = data.startTime
							this.screenName = data.screenName
							this.queryInfo()
				
						}
					})
				
			},
			getReFundRules() {
				refundRules().then(res => {
					const {
						data
					} = res
					const memberList = []
					const wxList = []
					data.map(item => {
						item.sortTime = item.ruleType == '<' ? parseInt(item.ruleTime) - 1 : parseInt(item
							.ruleTime) + 1
						if (item.returnType == 'REFUND_MEMBER') memberList.push(item)
						else wxList.push(item)
					})
					memberList.sort(function(a, b) {
						return a.sortTime - b.sortTime
					})
					wxList.sort(function(a, b) {
						return a.sortTime - b.sortTime
					})




					this.refundRulesList = [...memberList, ...wxList]


					this.setRefundFee()


				})
			},
			setRefundFee() {

				const diffInMinutes = Math.abs(moment(this.startTime).diff(moment(), 'minutes'));
				const orderPayType = this.memberRefundParams.cardCode ? 'REFUND_MEMBER' : 'REFUND_WX'
				let rule = {}
				this.refundRulesList.map(item => {
					//判断类型是否一样
					if (item.returnType == orderPayType) {

						if (item.ruleType == '<' && diffInMinutes < item.ruleTime) rule = item
						else if (item.ruleType == '>=' && diffInMinutes >= item.ruleTime) rule = item
					}
				})

				const ruleTypeText = rule.ruleType == '>=' ? '大于' : '小于'
				const num = this.priceList.length


				if (orderPayType == 'REFUND_MEMBER') {
					if (rule.feeType == 'FIX') this.memberRefundParams.fee = rule.fee * num
					else this.memberRefundParams.fee = this.orderTotal * (rule.fee / 100)
					this.refundTips =
						`${ruleTypeText}开场前${this.parseTime(rule.ruleTime)}，需扣除${this.memberRefundParams.fee}元手续费`
				} else {
					if (rule.feeType == 'FIX') this.ticketCode.fee = rule.fee * num
					else this.ticketCode.fee = this.orderTotal * (rule.fee / 100)
					this.refundTips =
						`${ruleTypeText}开场前${this.parseTime(rule.ruleTime)}，需扣除${this.ticketCode.fee}元手续费`
				}




			},
			queryInfo() {
				quertTicketList(this.quertTicketList).then(res => {
					const {
						data

					} = res
					// this.orderList = data

					const curOrder = data.find(item => item.orderCode == this.orderCode)
					this.oldOrderNo = curOrder.orderNo
					this.memberRefundParams.oldOrderNo = curOrder.orderNo
					this.priceList = curOrder.seatList.map(item => item.payPrice * 1)
					this.priceList.map(item => {
						this.orderTotal += parseFloat(item)
					})
					this.serviceFee = curOrder.seatList.map(item => item.serviceFee * 1)

					// this.getReFundRules();
				})
			},
			callCinema() {

				uni.makePhoneCall({
					phoneNumber: this.cinemaPhone
				})
			},
			refund() {
				this.setRefundFee();
				uni.showModal({
					content: this.refundTips,
					success: (result) => {
						if (result.confirm) {

							if (this.memberRefundParams.cardCode) {

								this.showPassword = true
								if (this.memberRefundParams.fee == null) {
									uni.showToast({
										title: '网络异常，请重试',
										icon: "none"
									})
									this.getReFundRules()
									return
								}

							} else {

								if (this.ticketCode.fee == null) {
									uni.showToast({
										title: '网络异常，请重试',
										icon: "none"
									})
									this.getReFundRules()
									return
								}
								wxRefundTicket(this.ticketCode).then(res => {
									const {
										code
									} = res
									if (code === "SUCCESS") {
										uni.showToast({
											title: '退票成功',
											icon: "none"
										})
										setTimeout(() => {
											uni.redirectTo({
												url: "../../pages/orderList/orderList"
											})
										}, 1500)
									} else {
										uni.showToast({
											title: "数据异常",
											icon: "none"
										})
									}
								})
							}
						}
					}
				})
			},
			checkTime(time) {
				const current = new Date().getTime()
				const filmTime = new Date(time).getTime()
				const refoundSwitch = this.$store.state.cinemaConfig.switchRefund
				if (filmTime > current && refoundSwitch == 1) this.isRefound = true

			},
			cardRefund() {
				if (!this.memberRefundParams.fee) {
					memberRefundTicket(this.memberRefundParams).then(res => {
						const {
							data,
							code
						} = res
						if (code === "SUCCESS") {
							uni.showToast({
								title: '退票成功',
								icon: "none"
							})
							setTimeout(() => {
								uni.redirectTo({
									url: "../../pages/orderList/orderList"
								})
							}, 1500)
						} else {
							uni.showToast({
								title: "数据异常",
								icon: "none"
							})
						}
					})

				} else {
					memberRefundTicketFee(this.memberRefundParams).then(_result => {
						if (_result.code === "SUCCESS") {
							wxPay({
								orderNo: _result.data.orderNo,
								openId: uni.getStorageSync("openId"),
								orderPhone: uni.getStorageSync('userPhone'),
							}).then(res => {
								const {
									data,
									code
								} = res
								if (code == 'SUCCESS') {
									uni.requestPayment({
										timeStamp: data.timeStamp,
										nonceStr: data.nonceStr,
										package: data.package,
										signType: data.signType,
										paySign: data.paySign,
										appId: data.appId,
										success: result => {
											if (result.errMsg ===
												"requestPayment:ok") {
												uni.showToast({
													title: '退票成功',
													icon: "none"
												})
												setTimeout(() => {
													uni.redirectTo({
														url: "../../pages/orderList/orderList"
													})
												}, 1500)
											}
										},
										fail: e => {
											uni.showToast({
												title: "网络异常，请重试",
												icon: 'none'
											})
										}
									})
								}
							})
						}

					})
				}


			},
			change(e) {
				this.memberRefundParams.password = e.detail

			},
			showCodeFn() {
				// const _this = this
				this.showCode = true
			}
		},
		computed: {
			resolveCode() {
				return code => {
					return code.slice(10, 19)
				}
			},
			getDate() {
				return date => {

					return date.slice(0, 10)
				}
			},
			getTime() {
				return date => {
					return date.slice(11, 16)
				}
			},
			parseTime() {
				return time => {
					const h = time / 60 | 0;
					const m = time % 60 | 0;
					return m != 0 ? `${h}小时${m}分钟` : `${h}小时`
				}
			},
			getName() {
				return code => {
					const {
						screens
					} = this.$store.state
					const index = screens.find(item => item.ScreenCode === code)
					return index ? index.ScreenName : ''
				}
			},
		},
	}
</script>

<style lang="scss">
	canvas {
		z-index: -1 !important;
	}



	.cinema-info {
		width: 90vw;
		margin: 20rpx auto 0 auto;
		padding: 30rpx 20rpx 30rpx 20rpx;
		background-color: #fff;
		border-radius: 20rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.refund-btn {
		position: relative;
		z-index: 9999;
		border: 1px solid royalblue;
		padding: 5rpx 15rpx;
		border-radius: 30rpx;
	}

	.rules-box {
		padding: 20rpx;
		position: relative;
		z-index: 999999;

		.rule-main-title {
			font-weight: bold;
			margin-bottom: 20rpx;
			text-align: center;
		}

		.rules-table {
			border: 1px solid #f0f0f5;

			.rules-title {
				display: flex;
				border-bottom: 1px solid #f0f0f5;
				padding: 10rpx 0;


				.rules-item {
					width: 20%;
					height: 60rpx;
					text-align: center;
					display: flex;
					align-items: center;
					justify-content: center;
					border-right: 1px solid #f0f0f5;

					&:nth-child(2) {
						width: 50%;
					}

					&:nth-child(3) {
						width: 30%;
						border-right: none;
					}
				}

			}
		}


	}

	.input-password {
		display: flex;
		flex-direction: column;
		align-items: center;
		width: 80vw;
		height: 200rpx;
		padding: 30rpx;
	}

	.ticket {
		width: 90vw;
		margin: -180rpx auto 0 auto;
		padding: 30rpx 20rpx 0 20rpx;
		background-color: #fff;
		border-radius: 20rpx;

		.ticket-header::after {
			content: '';
			width: 40rpx;
			height: 40rpx;
			background-color: #f0f0f5;
			position: absolute;
			border-radius: 50%;
			bottom: -20rpx;
			left: -50rpx;
		}

		.ticket-header::before {
			content: '';
			width: 40rpx;
			height: 40rpx;
			background-color: #f0f0f5;
			position: absolute;
			border-radius: 50%;
			bottom: -20rpx;
			right: -50rpx;
		}

		.ticket-header {
			border-bottom: 1px dashed #999;
			position: relative;

			.refund {
				width: 100%;
				display: flex;
				justify-content: space-between;
				padding-bottom: 5rpx;
				border-bottom: 1px solid #f0f0f5;

				text {
					// line-height: 60rpx;
					font-size: 26rpx;
					color: royalblue;
				}
			}

			.ticket-film {
				display: flex;
				justify-content: space-between;
				margin-top: 10rpx;

				.film-img {
					width: 155rpx;
					height: 190rpx;
				}

				.ticket-film-name {
					display: flex;
					flex-direction: column;

					// justify-content: space-between;

				}
			}

			.session {
				display: flex;
				margin-top: 20rpx;

				.session-date {
					display: flex;
					flex-direction: column;

					text {

						font-size: 28rpx;
					}

					text:first-child {
						font-size: 24rpx;
						color: #999;
					}
				}

				.session-screen {
					display: flex;
					flex-direction: column;

					margin-left: 150rpx;
					padding-bottom: 30rpx;

					.screen-name {
						font-size: 24rpx;
						color: #999;

					}

					.screen-seat {
						width: 380rpx;
						font-size: 28rpx;
						display: flex;
						flex-wrap: wrap;
						justify-content: flex-end;

						.seat-price {
							margin-left: 20rpx;
						}
					}
				}
			}
		}

		.film-code {
			position: relative;
			height: 220rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			border-bottom: 1px dashed #999;

			view {
				display: flex;
				flex-direction: column;
				font-size: 24rpx;
				color: #999;
				border: 1px solid #999;
				border-radius: 15rpx;
				padding: 10rpx 80rpx;

				text {
					line-height: 60rpx;
				}
			}
		}

		.film-code::after {
			content: '';
			width: 40rpx;
			height: 40rpx;
			background-color: #f0f0f5;
			position: absolute;
			border-radius: 50%;
			bottom: -20rpx;
			left: -50rpx;
		}

		.film-code::before {
			content: '';
			width: 40rpx;
			height: 40rpx;
			background-color: #f0f0f5;
			position: absolute;
			border-radius: 50%;
			bottom: -20rpx;
			right: -50rpx;
		}

		.film-qr {

			padding-bottom: 30rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			font-size: 28rpx;
			color: #999;

			.title {
				line-height: 60rpx;
			}

			.show-qr {
				font-size: 30rpx;
				color: black;
				display: flex;
				// justify-content: center;
				flex-direction: column;
				align-items: center;

				.qr-title {
					margin: 30rpx 0 50rpx 0;
					width: 88vw;

				}

				image {
					width: 250rpx;
					height: 250rpx;
				}
			}
		}
	}

	.close-eye {
		position: absolute;
		width: 40rpx;
		height: 40rpx;
		left: 180rpx;
		top: 20rpx;
	}
</style>